package com.ybkj.chat.module.main.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.ybkj.chat.R;
import com.ybkj.chat.base.BaseMvpActivity;
import com.ybkj.chat.manager.ActivityManager;
import com.ybkj.chat.manager.FullyGridLayoutManager;
import com.ybkj.chat.ui.adapter.GridImageAdapter;
import com.ybkj.chat.ui.view.recyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 陌生人动态发布图片
 */
public class SendPictureActivity extends BaseMvpActivity {

    //添加图片列表
    @BindView(R.id.add_recycle)
    XRecyclerView addRecycle;

    private GridImageAdapter gridImageAdapter;
    private List<LocalMedia> selectList = new ArrayList<>();
    private int selectSum = 3;
    private FullyGridLayoutManager manager;

    @Override
    protected void injectPresenter() {

    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_send_picture;
    }

    private GridImageAdapter.onAddPicClickListener onAddPicClick0 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(SendPictureActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(selectSum)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(0);
        }
    };

    @Override
    protected void initView() {
        gridImageAdapter = new GridImageAdapter(this, onAddPicClick0);
        manager = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        addRecycle.setLayoutManager(manager);
        addRecycle.setAdapter(gridImageAdapter);
    }

    @Override
    protected void initData() {

    }

    /**
     * 结果回调
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0:
                    // 图片选择结果回调
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList.addAll(images);
//                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    gridImageAdapter.setList(selectList);
                    gridImageAdapter.notifyDataSetChanged();
                    break;
            }
        }
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.left_tv, R.id.right_tv, R.id.location_tv, R.id.look_tv, R.id.friend_share, R.id.qq_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //取消
            case R.id.left_tv:
                finish();
                break;
            //发布
            case R.id.right_tv:
                break;
            //所在位置
            case R.id.location_tv:
                ActivityManager.gotoActivity(mContext, locationActivity.class);
                break;
            //谁可以看
            case R.id.look_tv:
                ActivityManager.gotoActivity(mContext, WhoLookActivity.class);
                break;
            //微信朋友圈分享
            case R.id.friend_share:
                ActivityManager.gotoActivity(mContext, FriendGroupShareActivity.class);
                break;
            //QQ空间分享
            case R.id.qq_share:
                break;
        }
    }
}
